<%@ page import="com.wl.shixun2.entity.Product" %>
<%@ page import="com.wl.shixun2.dao.ProductDAO" %>
<%@ page import="java.sql.Timestamp" %>
<%@ page import="java.util.List" %>
<%@ page import="java.net.URLEncoder" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    // 设置请求字符编码为UTF-8，解决中文参数乱码问题
    request.setCharacterEncoding("UTF-8");
%>
<html>
<head>
    <title>编辑产品 - 2350309097 万朗</title>
    <link rel="stylesheet" href="css/editProduct.css" type="text/css">
</head>
<body>
<h2>编辑产品</h2>

<%
    String idParam = request.getParameter("id");
    Product product = null;
    String error = null;
    
    if (idParam != null && !idParam.trim().isEmpty()) {
        try {
            long id = Long.parseLong(idParam);
            List<Product> productList = ProductDAO.findById(id);
            if (productList != null && !productList.isEmpty()) {
                product = productList.get(0);
            } else {
                error = "未找到ID为 " + id + " 的产品";
            }
        } catch (NumberFormatException e) {
            error = "无效的产品ID格式";
        } catch (Exception e) {
            error = "获取产品信息时出错: " + e.getMessage();
        }
    } else {
        error = "未提供产品ID";
    }
    
    // 处理表单提交
    if (request.getMethod().equals("POST") && idParam != null && !idParam.trim().isEmpty()) {
        try {
            long id = Long.parseLong(idParam);
            String number = request.getParameter("number");
            String name = request.getParameter("name");
            String priceStr = request.getParameter("price");
            String madetimeStr = request.getParameter("madetime");
            
            // 准备更新参数（只有非空值才传递）
            String updateNumber = (number != null && !number.trim().isEmpty()) ? number.trim() : null;
            String updateName = (name != null && !name.trim().isEmpty()) ? name.trim() : null;
            Integer updatePrice = null;
            Timestamp updateMadetime = null;
            
            // 处理价格
            if (priceStr != null && !priceStr.trim().isEmpty()) {
                try {
                    updatePrice = Integer.parseInt(priceStr.trim());
                } catch (NumberFormatException e) {
                    error = "价格格式不正确";
                }
            }
            
            // 处理生产日期
            if (madetimeStr != null && !madetimeStr.trim().isEmpty()) {
                try {
                    String datetimeStr = madetimeStr.replace("T", " ") + ":00";
                    updateMadetime = Timestamp.valueOf(datetimeStr);
                } catch (Exception e) {
                    error = "生产日期格式不正确";
                }
            }
            
            if (error == null) {
                if (ProductDAO.updatePartial(id, updateNumber, updateName, updatePrice, updateMadetime)) {
                    response.sendRedirect("productList.jsp?message=" + URLEncoder.encode("更新成功", "UTF-8"));
                    return;
                } else {
                    error = "没有字段需要更新或更新失败";
                }
            }
        } catch (NumberFormatException e) {
            error = "无效的产品ID格式";
        } catch (Exception e) {
            error = "更新产品时出错: " + e.getMessage();
        }
    }
%>

<% if (error != null) { %>
    <div class="error"><%= error %></div>
<% } %>

<% if (product != null) { %>
<form method="post" action="">
    <input type="hidden" name="id" value="<%= product.getId() %>">
    
    <div class="form-group">
        <label for="id">产品ID:</label>
        <input type="text" id="id" name="id" value="<%= product.getId() %>" readonly>
    </div>
    
    <div class="form-group">
        <label for="number">产品号:</label>
        <input type="text" id="number" name="number" placeholder="<%= product.getNumber() != null ? product.getNumber() : "请输入产品号" %>">
    </div>
    
    <div class="form-group">
        <label for="name">名称:</label>
        <input type="text" id="name" name="name" placeholder="<%= product.getName() != null ? product.getName() : "请输入产品名称" %>">
    </div>
    
    <div class="form-group">
        <label for="price">价格:</label>
        <input type="number" id="price" name="price" placeholder="<%= String.valueOf(product.getPrice()) %>">
    </div>
    
    <div class="form-group">
        <label for="madetime">生产日期:</label>
        <input type="datetime-local" id="madetime" name="madetime">
        <% if (product.getMadetime() != null) { %>
            <span class="current-value">（当前值：<%= product.getMadetime().toString().substring(0, 16) %>）</span>
        <% } %>
    </div>
    
    <div class="form-group">
        <input type="submit" value="更新产品">
        <input type="button" class="cancel-btn" value="取消" onclick="window.location.href='productList.jsp'">
    </div>
</form>
<% } else { %>
    <p>
        <a href="productList.jsp">返回产品列表</a> | 
        <a href="index.jsp">返回主界面</a>
    </p>
<% } %>
</body>
</html>